//
//  AddView.m
//  BigJoy
//
//  Created by Rowling on 2017/7/18.
//  Copyright © 2017年 Rowling. All rights reserved.
//

#import "AddView.h"

@interface AddView()
@property (nonatomic,strong) NSMutableArray * buttonArray;
@end

@implementation AddView

-(NSMutableArray *)buttonArray {
    if (_buttonArray == nil) {
        _buttonArray = [[NSMutableArray alloc]init];
    }
    return _buttonArray;
}

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setUI];
    }
    return self;
}

-(void)setUI {
    self.backgroundColor = [UIColor clearColor];
    [self addbur];
    [self setButton];
    UIButton * backBtn = [XMSuperHelper XMbuttonWithFrame:CGRectMake(0, 17, 50, 50) Tile:@"" FontSize:17 selector:@selector(backBtnClick) Target:self];
    [backBtn setImage:[UIImage imageNamed:@"fanhuibaiyouyiny"] forState:UIControlStateNormal];
    [self addSubview:backBtn];
}

-(void)backBtnClick {
    [self animationDismiss];
}

-(void)addbur {
    // 加入毛玻璃效果
    UIBlurEffect * blurE = [UIBlurEffect effectWithStyle:UIBlurEffectStyleRegular];
    UIVisualEffectView * blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurE];
    blurEffectView.frame = self.bounds;
//    blurEffectView.alpha = ;
    [self insertSubview:blurEffectView atIndex:0];
}

-(void)setButton {
    NSArray * imgArray = @[@"tianjiazhaop",@"tianjiachahua",@"tianjiawenzi",@"tianjiasucai"];
    NSArray * titleArray = @[@[@"PHOTO",@"添加照片"],@[@"PICTURE",@"添加插画"],@[@"TYPEFACE",@"添加文字"],@[@"MATERIAL",@"添加素材"]];
    CGFloat baseH = XMW(69);
    for(int i = 0;i < 4; i ++) {
        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - XMW(292), baseH + (8+XMW(127)) * i, XMW(292), XMW(127))];
        [self addSubview:view];
        UIImageView * imageView = [UIImageView new];
        imageView.image = [UIImage imageNamed:imgArray[i]];
        [view addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.equalTo(view);
        }];
        UILabel * enTitle = [XMSuperHelper XMLableWithColor:[UIColor whiteColor] Title:titleArray[i][0] FontSize:20];
        UILabel * chTitle = [XMSuperHelper XMLableWithColor:[UIColor whiteColor] Title:titleArray[i][1] FontSize:18];
        [view addSubview:enTitle];
        [view addSubview:chTitle];
        [enTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(view).offset(15);
        }];
        [chTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(view).offset(-20);
            make.left.equalTo(view).offset(15);
        }];
        
        UIView * lineView = [UIView new];
        [self addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(chTitle.mas_bottom).offset(1);
            make.left.equalTo(view).offset(15);
            make.width.equalTo(chTitle);
            make.height.equalTo(@1.5);
        }];
        lineView.backgroundColor = [UIColor whiteColor];
        [self.buttonArray addObject:view];
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
        [view addGestureRecognizer:tap];
        view.userInteractionEnabled = YES;
        view.tag = 100 + i;
    }
}

-(void)tap:(UITapGestureRecognizer *)tap {
    NSInteger tag = tap.view.tag - 100;
    if (self.ButtonClickBlock) {
        self.ButtonClickBlock(tag);
    }
    [self animationDismiss];
}

-(void)animationAddToView:(UIView *)view {
    [view addSubview:self];
    int i = 0;
    for (UIView * view in self.buttonArray) {
        CGFloat startX = view.x;
        view.x = SCREEN_WIDTH;
        [self layoutIfNeeded];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * i * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [UIView animateWithDuration:0.25 animations:^{
                view.x = startX;
                [self layoutIfNeeded];
            }];
        });
        i++;
    }
}

-(void)animationDismiss {
    int i = 0;
    BOOL close = NO;
    for (UIView * view in self.buttonArray) {
        if (i == 3) {
            close = YES;
        }
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * i * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [UIView animateWithDuration:0.25 animations:^{
                view.x = SCREEN_WIDTH;
                [self layoutIfNeeded];
            } completion:^(BOOL finished) {
                if (close) {
                    [self removeFromSuperview];
                }
            }];
        });
        i ++;
    }
}

//-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    [self animationDismiss];
//}

-(void)dealloc {
    NSLog(@"view dealloc");
}

@end
